"""
@author: LiuJun
@file: 链表反转.py
@time: 2022/12/4 15:22
"""


class ListNode:
    def __init__(self, val, next=None):
        if isinstance(val, int):
            self.val = val
            self.next = next


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head == None or head.next == None:
            return head
        newHead = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return newHead


def jc(n):
    result = ListNode(1)
    for i in range(2,n+1):
        result = ListNode(i,result)
    return result


list_node = jc(9)
# while list_node:
#     print(list_node.val)
#     list_node = list_node.next
obj = Solution()
reve_list_node = obj.reverseList(list_node)
while reve_list_node:
    print(reve_list_node.val)
    reve_list_node = reve_list_node.next
